);
}
+ $requestMethod = $request->getMethod();
$matchers = $this->getMatchers();
- $matcher = $matchers[$request->getMethod()] ?? null;
+ $matcher = $matchers[$requestMethod] ?? null;
$match = $matcher ? $matcher->match( $relPath ) : null;
+ // For a HEAD request, execute the GET handler instead if one exists.
+ // The webserver will discard the body.
+ if ( !$match && $requestMethod === 'HEAD' && isset( $matchers['GET'] ) ) {
+ $match = $matchers['GET']->match( $relPath );
+ }
+
if ( !$match ) {
// Check for 405 wrong method
$allowed = [];
foreach ( $matchers as $allowedMethod => $allowedMatcher ) {
- if ( $allowedMethod === $request->getMethod() ) {
+ if ( $allowedMethod === $requestMethod ) {
continue;
}
if ( $allowedMatcher->match( $relPath ) ) {
if ( $allowed ) {
$response = $this->responseFactory->createLocalizedHttpError( 405,
( new MessageValue( 'rest-wrong-method' ) )
- ->textParams( $request->getMethod() )
+ ->textParams( $requestMethod )
->commaListParams( $allowed )
->numParams( count( $allowed ) )
);
$this->assertSame( 'GET', $response->getHeaderLine( 'Allow' ) );
}
+ public function testHeadToGet() {
+ $request = new RequestData( [
+ 'uri' => new Uri( '/rest/user/joe/hello' ),
+ 'method' => 'HEAD'
+ ] );
+ $router = $this->createRouter( $request );
+ $response = $router->execute( $request );
+ $this->assertSame( 200, $response->getStatusCode() );
+ }
+
public function testNoMatch() {
$request = new RequestData( [ 'uri' => new Uri( '/rest/bogus' ) ] );
$router = $this->createRouter( $request );